package com.lkm.app_ui.ui.fj;

import android.os.Looper;

import androidx.annotation.NonNull;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.RouterUINavigation;
import com.lkm.app_ui.RunTimeData;
import com.lkm.comlib.ui.BaseFragmentActivity;
import com.lkm.frame.otto.LinkBus;

public class FjApp {
    public static final FjApp install = new FjApp();


    private FjWaitOrder fjWaitOrder = new FjWaitOrder();
    

    @NonNull
    private FjInfo fjInfo = new FjInfo();



    public void init() {
        fjWaitOrder.init();
        AppConst.mainHandler.post(() -> upFjInfo(fjInfo));
    }

    public void upFjInfo(@NonNull FjInfo fj_Info) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            AppConst.mainHandler.post(() -> {
                upFjInfo(fj_Info);
            });
            return;
        }
        fjInfo = fj_Info;
        LinkBus.sendBroadcast(new OnFjInfoEvent());
    }

    @NonNull
    public FjWaitOrder getFjWaitOrder() {
        return fjWaitOrder;
    }

    @NonNull
    public FjInfo getFjInfo() {
        return fjInfo;
    }

    

    public boolean fjItemWeight(String realWeight) {
        FjOrder.FjItem fjItem = fjInfo.getCurFjItem();
        if (fjItem == null) return false;
        
        fjItem.realWeight = realWeight;

        FjOrder fjOrder = fjInfo.getCurFjOrder();

        if (fjOrder == null) {
            goFjOrderFragment();
            return false;
        }
        
        fjInfo.setCurFj(fjOrder, fjOrder.findNext(null));
        upFjInfo(fjInfo);
        return true;
    }

    

    public synchronized void waitOrder() {
        fjWaitOrder.putWaitOrder(fjInfo.getCurFjOrder());
        fjInfo.setCurFj(null, null);
        
    }


    

    private void goFjOrderFragment() {

        RouterUINavigation.getInstance().showFjOrder(BaseFragmentActivity.getLastActivity(), false);

    }

    public boolean isEnable() {
        try {
            return RunTimeData.steelyardInfo.upDataApiTo.isFjEnable();
        }catch (Exception e){}
        return false;
    }

    

    public boolean isFjing() {
        return fjInfo.getCurFjOrder() != null;
    }
}
